package com.atguigu.gulimall.search.config;

import lombok.Setter;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.protocol.HttpContext;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @NAME: GulimallElasticSearchConfig
 * @USER: wjj
 * @DATE: 2023/9/14
 * @TIME: 11:22
 * @DAY_NAME_FULL: 星期四
 * @PROJECT_NAME: gulimall
 */
/*
 *1、导入依赖
 * 2、编写配置
 *3、参照官方文档https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
 * */
//自定义连接地址，结合配置类@ConfigurationProperties(prefix = "elasticsearch")，从yaml获取port，host

//@ConfigurationProperties(prefix = "elasticsearch")
@Setter
@Configuration
public class GulimallElasticSearchConfig {

//    private String host;
//
//    private Integer port;
     @Bean
     public RestHighLevelClient esRestClient(@Value("${elasticsearch.host}") String host,@Value("${elasticsearch.port}") Integer port){
         System.out.println(host+"++++++++++++++++++++");
         RestHighLevelClient client = new RestHighLevelClient(
                 RestClient.builder(new HttpHost(host, port, "http")
                 ).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                     @Override
                     public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                         return requestConfigBuilder.setConnectTimeout(90000000)
                                 .setSocketTimeout(90000000);
                     }
                 }).setHttpClientConfigCallback((httpAsyncClientBuilder -> {
                     httpAsyncClientBuilder.disableAuthCaching();//禁用身份验证缓存

                     //显式设置keepAliveStrategy
                     httpAsyncClientBuilder.setKeepAliveStrategy((HttpResponse, HttpContext)-> TimeUnit.MINUTES.toMillis(3));
                     //显式开启tcp keepalive
                     httpAsyncClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(true).build());
                     return httpAsyncClientBuilder;

                 }))
         );
         return  client;
     }

    public static final RequestOptions COMMON_OPTIONS;
    static {

        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        // builder.addHeader("Authorization", "Bearer " + TOKEN);
        // builder.setHttpAsyncResponseConsumerFactory(
        //         new HttpAsyncResponseConsumerFactory
        //                 .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }



}
